/**
 * s_mhz.c
 * MHz source for TrayInfo.
 * MHz support added by Laszlo Ashin <kodest@gmail.com>
 */

#include <stdio.h>
#include <stdlib.h>

#include "trayinfo.h"
#include "utils.h"

static int
mhz_write(struct timeval *now, int fd)
{
	float mhz = 0;
	const char *idlist[] = { "cpu MHz" };
	char *vallist[] = { NULL };

	collect_data_from_file(
		"/proc/cpuinfo",
		":", 1, idlist, vallist
	);
	if (*vallist) {
		if (sscanf(*vallist, "%f", &mhz) != 1)
			mhz = 0;

		free(*vallist);
	}
	if (mhz > 1.0f) {
		printf("%.0fMHz", mhz);
		return 1;
	}
	return 0;
}

void
regsrc_mhz(src_t *src)
{
	src->name = "mhz";
	src->write = mhz_write;
}
